package com.pt.utils
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.core.UIComponent;
	
/* 	import uk.gov.fsa.frm.common.model.FRMGlobalConstants;
	import uk.gov.fsa.frm.common.model.UserInstance;
	import uk.gov.fsa.frm.facade.ApplicationFacade;
	import uk.gov.fsa.frm.facade.ApplicationNotificationConstants;
	import uk.gov.fsa.frm.frmMain.view.LeftNavigationMediator;
	 */
	public class TimerController
	{
		public static const STOP_AUTO_SAVE_TIMER:String='STOP_AUTO_SAVE_TIMER';
		public static const START_AUTO_SAVE_TIMER:String='START_AUTO_SAVE_TIMER';  
		public var AUTO_SAVE_TIME:Number=60000 ;
		private var _timer : Timer = new Timer(AUTO_SAVE_TIME);
		//private var _facade : ApplicationFacade = ApplicationFacade.getInstance();
		
		public function TimerController( application : UIComponent )
		{
			_timer.addEventListener(TimerEvent.TIMER, onTimerComplete);
			//application.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
			//application.addEventListener(MouseEvent.CLICK, onMouseClick);
			application.addEventListener(TimerController.STOP_AUTO_SAVE_TIMER,stopTime);
			application.addEventListener(TimerController.START_AUTO_SAVE_TIMER,startTime);
			trace('Time'+_timer);
		}

		private function stopTime(e:Event):void{
			_timer.stop();
		}
		
		//_facade.FRMApplication.mainfrmview.currentSelectedIndex == MainFRMView.ASSESSMENT_MAIN_VIEW_INDEX
		public function startTimer() : void
		{
			
           /*  if(num !=0){ */
/*             	if(UserInstance.instance.loggedInUserDetails.permLvl != FRMGlobalConstants.READ_ONLY
            	&& UserInstance.instance.loggedInUserDetails.autosaveInd == 1){
 */	            	//AUTO_SAVE_TIME = 60000 * Number(UserInstance.instance.loggedInUserDetails.autosavePeriod);
	            	/*  var userTime:Number = Number(UserInstance.instance.loggedInUserDetails.autosavePeriod);
	            	 if( userTime != 0){
			             _timer.delay = 60000 *userTime; 
			             trace(_timer.currentCount);
			             _timer.start();
	            	 } */
            	//}
          /*   } */
        }



        public function resetAndStartTimer() : void
        {
        	_timer.reset();
        	startTimer();
        }
		private function startTime(e:Event):void{
			_timer.reset();
        	startTimer();
		}
        private function onTimerComplete( event: TimerEvent ):void 
        
        {
        	var num:Number =0;
			 //num = (_facade.retrieveMediator(LeftNavigationMediator.NAME)as LeftNavigationMediator).globalArrayLength;
			 if(num !=0){
	         //	_facade.sendNotification(ApplicationNotificationConstants.FRM_START_AUTO_SAVE,_facade.FRMApplication);
			 }else{
			 	resetAndStartTimer();
			 }
			 
        }
        
		private function onKeyDownHandler( event : KeyboardEvent ) : void
		{
			resetAndStartTimer();
		}
		
		private function onMouseClick( event : MouseEvent ) : void
		{
			//resetAndStartTimer();
		}
        
	}
}